define(function(require, exports, module) {
    'use strict';

    var rk = require('rk');



    // require('globaljs');
    $.widget('rk.groupSetCtrl', $.rk.oaDialogCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },
        _listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var showDiv = elem.find("[act='group_icon_div']");
            var newSrc = elem.find("[act='group_icon_url']");
            var $picUploadBtn = $("#group_icon_sel_file").uploadPic({
                uploadify_setting: {
                    uploader: '/json/global_uploadify/img-upload.action',
                    buttonText: rk.i18n('GROUP_ICON_SELECT'),
                    buttonClass: 'uploadify-button file_select',
                    queueID: 'iconFileQueue',
                    removeCompleted: true,
                    multi: false,
                    removeTimeout: 0,
                    onUploadSuccess: function(file, data, response) {
                        var json = $.parseJSON(data);
                        if (json.status == 0) {
                            showDiv.html('<img src="' + json.data.returnParams.fileSUrl + '" alt="" act="group_icon">');
                            newSrc.val(json.data.returnParams.fileSUrl);
                        }
                    },
                    onUploadStart: function(file) {
                        $picUploadBtn.uploadify('settings', 'formData', {
                            'pageTenantId': SESSION.tenantId,
                            'uploadFileName': file.name,
                            'pageUserId': SESSION.user.id
                        });
                    }
                }
            });
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            require.async([opt.type == "depart" ? 'oa/tmpl/group/content/tpl_depart_set.tpl' : 'oa/tmpl/group/content/tpl_group_set.tpl'], function(temp) {
                rk.post("/json/oa_group/popedit.action", {
                    gid: opt.gid
                }, function(json) {
                    if (json.status === 0) {
                        var html = rk.templateText(temp, json.group);
                        elem.html(html);
                        me.tenantId = json.tenantId;
                        me.userId = json.userId;
                        me._listeners();
                        //groupPermission
                        if (elem.find("[act='groupPermission']")) {
                            elem.find("[act='groupPermission']").buttonset();
                        }
                    } else if (json.status === 217001) {
                        rk.noticeError(rk.i18n('GROUP_NO_PERMISSION'));
                    } else {
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                    rk.repositionDialog();
                });
            });
            var footer = me.getFooter();
            me.getBtn('ok').on('click', $.proxy(me.okClicked, me));
            footer.find('*[act="cancel"]').on('click', $.proxy(me.cancelClicked, me));
        },
        okClicked: function() {
            var me = this;
            var elem = me.element;
            me._doSave(); //保存
        },
        cancelClicked: function() {
            var me = this;
            var elem = me.element;
            me.closeMe(); //关闭
        },
        _doSave: function() {
            //setting
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var form = elem.find("[act='group_update_set_from']");
            var datas = form.serializeArray();
            var params = {};
            $.each(datas, function(i, json) {
                params[json.name] = json.value;
            });
            if (opt.type != "depart") {
                if (!params.groupName) {
                    rk.noticeError(rk.i18n('GROUP_PLEASE_INPUT_GROUP_NAME'));
                    return;
                }
            }
            me.getBtn('ok').beLoading();
            rk.post("/json/oa_group/setting.action", params, function(json) {
                if (json.status === 0) {
                    //刷新 colleague_depart_index_about  colleague_depart_index_icon
                    //rk.content
                    var content = rk.oa.getContent();
                    if (content && content.length > 0) {
                        if (opt.type == "depart") {
                            if (content && content.find("[act='colleague_depart_index_about']")) {
                                content.find("[act='colleague_depart_index_about']").text(json.group.about);
                            }
                            if (content && content.find("[act='colleague_depart_index_icon']")) {
                                if (json.group.icon) {
                                    content.find("[act='colleague_depart_index_icon']").html("<img src='" + json.group.icon + "'/>");
                                }
                            }
                        } else {
                            if (content && content.find("[act='group_content_name']")) {
                                content.find("[act='group_content_name']").text(json.group.name);
                            }
                            if (content && content.find("[act='group_content_index_about']")) {
                                content.find("[act='group_content_index_about']").text(json.group.about);
                            }
                            if (content && content.find("[act='group_content_index_icon']")) {
                                content.find("[act='group_content_index_icon']").html("<img src='" + json.group.icon + "'/>");
                            }
                            if (content && content.find("[act='group_is_private']")) {
                                if (json.group.permission === 1) {
                                    content.find("[act='group_is_private']").show();
                                } else {
                                    content.find("[act='group_is_private']").hide();
                                }
                            }
                            var panel = rk.oa.getPanel();
                            if (panel) {
                                require.async(["../groupPanelCtrl"], function() {
                                    if (opt.loadParam) {
                                        panel.groupPanelCtrl("loaddata", opt.loadParam.type, opt.loadParam.param);
                                    }
                                });
                            }
                        }
                    } else {
                        history.go(0);
                    }
                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                    me.closeMe();
                } else if (json.status === 217001) {
                    rk.noticeError(rk.i18n('GROUP_NO_PERMISSION'));
                    me.getBtn('ok').beLoading('delay');
                } else {
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    me.getBtn('ok').beLoading('delay');
                }
            });
        }
    });
});
